package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.CtfContainer;
import com.woniu.entity.CtfContest;
import com.woniu.entity.vo.CtfContestSituation;
import com.woniu.entity.vo.CtfContestVO;
import com.woniu.service.CtfContestService;
import com.woniu.mapper.CtfContestMapper;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author nanqiao
* @description 针对表【ctf_contest(考试)】的数据库操作Service实现
* @createDate 2025-06-10 12:15:44
*/
@Service
public class CtfContestServiceImpl extends ServiceImpl<CtfContestMapper, CtfContest>
    implements CtfContestService{
    @Autowired
    private final CtfContestMapper ctfContestMapper;

    public CtfContestServiceImpl(CtfContestMapper ctfContestMapper) {
        this.ctfContestMapper = ctfContestMapper;
    }

    @Override
    public List<CtfContainer> getContainersByContestId(Integer contestId) {
        return ctfContestMapper.selectContainersByContestId(contestId);
    }

    @Override
    public List<CtfContest> selectContestsByUserId(@ToolParam(description = "用户id") Integer userId) {
        return ctfContestMapper.selectContestsByUserId(userId);
    }

    /**
     * @Description 新增：实现搜索考试的方法
     * @Param
     * @Return
     * @Author pml
     * @Date 2025/6/12 10:30
     */
    @Override
    public List<CtfContest> searchContests(String keyword) {
        QueryWrapper<CtfContest> wrapper = new QueryWrapper<>();
        wrapper.like("contest_name", keyword)      // 搜索考试名称
                .or().like("contest_type", keyword) // 搜索考试类型
                .or().like("contest_status", keyword) // 搜索考试状态
                .or().like("contest_difficulty", keyword); // 搜索难度
        // 通过Mapper执行查询
        return ctfContestMapper.selectList(wrapper);
    }

    /**
     * @Author zhaoj
     * @Date 2025/6/16 19:48
     * @Description 考试表多条件查询与分页
     * @Params
     * @Return
     **/
    @Override
    public Object queryByArgs(CtfContestVO contest) {
        IPage<CtfContest> page = new Page<>(contest.getPageCurrent(),contest.getPageSize());
        QueryWrapper<CtfContest> wrapper = new QueryWrapper<>();
        if(contest.getContestName() != null && !contest.getContestName().isEmpty()){
            wrapper.like("contest_name", contest.getContestName());
        }
        if(contest.getContestType() != null && !contest.getContestType().isEmpty()){
            wrapper.like("contest_type", contest.getContestType());
        }
        if(contest.getContestStatus() != null && !contest.getContestStatus().isEmpty()){
            wrapper.like("contest_status", contest.getContestStatus());
        }
        if(contest.getContestDifficulty() != null){
            wrapper.eq("contest_difficulty", contest.getContestDifficulty());
        }
        if(contest.getContestId() != null){
            wrapper.eq("contest_id", contest.getContestId());
        }
        //后续可以配置VO实现时间范围查询
        return ctfContestMapper.selectPage(page,wrapper);
    }

    @Override
    public List<CtfContestSituation> searchContestByUserName(String userName) {
        return ctfContestMapper.selectContestByUserName(userName);
    }
}




